package com.zxing.demo;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;


public class Decode_Zxing {
    public static void main(String[] args) {
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        File file = new File("d:/img.png");
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));

        //定义二维码的cans
        try {
            HashMap hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            Result result = multiFormatReader.decode(binaryBitmap,hints);
            System.out.println("解析结果：" + result.toString());
            System.out.println("二维码类型：" + result.getBarcodeFormat());
            System.out.println("二维码文本内容：" + result.getText());
        } catch (NotFoundException e) {
            e.printStackTrace();
        }

        System.out.println();
    }
}
